<?php
// +----------------------------------------------------------------------
// | snake
// +----------------------------------------------------------------------
// | Copyright (c) 2016~2022 http://baiyf.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: NickBai <1902822973@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\model;

use think\Model;



use think\Db;

class WeiboModel extends Model

{
    // 确定链接表名
    protected $table = 'wb_message';

    /**
     * 根据搜索条件获取角色列表信息
     * @param $where
     * @param $offset
     * @param $limit
     */
    public function getWeiboByWhere($where, $offset, $limit)
    {
        return $this->where($where)->limit($offset, $limit)->order('id desc')->select();
//        return '11111';
    }

    /**
     * 根据搜索条件获取所有的微博数量
     * @param $where
     */
    public function getAllWeibo($where)
    {
        return $this->where($where)->count();
    }

    /**
<
     * 删除微博
     * @param $id
     */
    public function delWeibo($id)
    {
        try {

            $this->where('id', $id)->delete();
            return msg(1, '', '删除微博消息成功');

        } catch (PDOException $e) {

            return msg(-1, '', $e->getMessage());
        }
    }



//获取微博详情
    public function getOneWeibo($id)
    {
//        $res = $this->where('id', $id)->find();
        $list = Db::field('u.username un, m.content content,m.time time,collectnum,commentnum,transpondnum,agreenum,readnum,m.id id,src_id')
            ->table(['wb_buser' => 'u', 'wb_message' => 'm'])
            ->where('u.id = m.uid and m.id = ' . $id)
            ->find();

        if (!empty($list['src_id'])) {
//                halt($v['src_id']);
            $res = Db::field('m.id id,u.username username,photo,content,time')
                ->table(['wb_message' => 'm', 'wb_buser' => 'u', 'wb_buserinfo' => 'i'])
                ->where('m.uid = u.id and  i.uid = u.id  and  m.id = ' . $list['src_id'])
                ->find();
//            halt($res);
            $list['old'] = $res;
            $path = Db::field('path')
                ->table(['wb_mpicture' => 'mp'])
                ->where('mp.mid = ' . $list['src_id'])
                ->find();
//                halt($path);
            if (!empty($path)) {
                $list['old']['path'] = $path['path'];
            }
        }


//        halt($list);
        return $list;

//        halt($res);

    }



}